#!/usr/bin/env perl

use utf8;
use strict;
use warnings;
use English;
use FindBin;
use lib "$FindBin::Bin/../lib";
use DBDefs;
use Sql;
use aliased 'MusicBrainz::Server::DatabaseConnectionFactory' => 'Databases';

if ($ARGV[0])
{
    my $dbname = uc($ARGV[0]);

    my $db = Databases->get($dbname);
    die "There is no configuration in DBDefs for database $dbname"
        unless defined $db;

    my $dbh = Databases->get_connection($dbname);
    $dbh->sql->quiet(1);

    eval {
        my $so = $dbh->sql->select_single_value(<<~'SQL', $db->database);
            SELECT TRUE
              FROM information_schema.information_schema_catalog_name
             WHERE catalog_name = ?
            SQL
        exit 0 if $so;
    };

    my $error = $EVAL_ERROR;

    if (ref $error eq 'MusicBrainz::Server::Exceptions::DatabaseError') {
        my $expected_error = 'database "' . $db->database . '" does not exist';
        if (
            # Note that 08006 is returned for any connection failure,
            # not just "database [...] does not exist."
            $error->sqlstate eq '08006' &&
            $error->message =~ /\Q$expected_error\E$/
        ) {
            exit 1; # E_NODATABASE
        }
    }

    die $error;
}
else
{
    print "Usage: database_exists <DBNAME>\n";
    print "\n";
    print "\tDBNAME\tThe database name (READWRITE or TEST)\n";
    print "\n";
    exit 2;
}
